using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace XNAGame_Lab1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        //        List<MyXNAButton> myButton = new List<MyXNAButton>();
        Map map;
//        Zoombie animal;
  //      int nMyButton;
        List<Zoombie> button;
        Sun sun;
        MyText2D text;
        MapWithCell mapcell;
       Store myStores;
    //   Zoombie right;
        //Store store;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            this.IsMouseVisible = true;
            graphics.PreferredBackBufferWidth = 1000;
            graphics.PreferredBackBufferHeight = 600;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            button = new List<Zoombie>();
            myStores = new Store();

            // TODO: use this.Content to load your game content here

            //          InitNewButton(out str, out button, "start1", new Vector2(0, 0), new Vector2(50, 50));
            //        InitNewButton(out str, out button, "exit1", new Vector2(0, button.TopLeft.Y + 50), new Vector2(50, 50));
            //      InitNewButton(out str, out button, "resume1", new Vector2(0, button.TopLeft.Y + 50), new Vector2(50, 50));
            string[] strCellTexture = new string[100];
            for (int i = 0; i < 100; i++)
                strCellTexture[i] = "khungbaotrongsuot00";
            //            mapcell = new MapWithCell();
            int nRow = 12;
            int nCol = 5;
            int[,] cell = new int[12, 5];
            // //           Random r = new Random();
            int k = 0;
            for (int i = 0; i < nRow; i++)
                for (int j = 0; j < nCol; j++)
                {
                    cell[i, j] = k++;
                    //                   Cell cellindex = new Cell(Content,"khungbao00",new Vector2(i * 66+100, j*72+150), new Vector2(66, 72));
                }
            mapcell = new MapWithCell(Content, strCellTexture, 12 * 5, 12, 5, cell, new Vector2(100, 150), new Vector2(66, 72));
            ////                
            //  //              "kecleon", new Vector2(0, 0), new Vector2(100, 100));
            //          //  myBTN = new MyXNAButton(Content, "Knight", 15, new Vector2(0, 0), new Vector2(50, 50));
            //            //            _nSprites = 1;
            //            //      Random rd = new Random();
            //            //            for (int i = 0; i < _nSprites; i++)
            map = new Map(Content, "main", new Vector2(0, 0), new Vector2(1000, 600));
//            animal = new Zoombie(Content, "thu", 17, new Vector2(100, 100), new Vector2(65, 65),10,sun);
            MyXNAButton store1 = new MyXNAButton(Content, "giay", 1, new Vector2(255, 5), new Vector2(65, 65));
            MyXNAButton store2 = new MyXNAButton(Content, "duahau", 1, new Vector2(315, 5), new Vector2(65, 65));
            MyXNAButton store3 = new MyXNAButton(Content, "bachtuot", 1, new Vector2(375, 5), new Vector2(65, 65));
            MyXNAButton store4 = new MyXNAButton(Content, "zoombie", 1, new Vector2(435, 5), new Vector2(65, 65));
            
      //      right = new Zoombie(Content,"thu",15,new Vector2(500,200),new Vector2(50,50),0,null);




//            store1.OnPress+=new EventHandler(myXNAButton_OnPress);
            myStores.Add(store1);
            myStores.Add(store2);
            myStores.Add(store3);
            myStores.Add(store4);
         //   sun = new Sun(Content, "sun00", 2, new Vector2(0, 0), new Vector2(50, 50));
            string textname = "0";
            SpriteFont  Font1 = Content.Load<SpriteFont>("font");
            text = new MyText2D(Content, textname, Font1,new Vector2(150,43));
            sun = new Sun(Content, "giay", 1, new Vector2(0, 0), new Vector2(50, 50));
            
         // ZoombieStore   zbs = new ZoombieStore();
           
          //  store = new Store();
            //store.AddMyXNAButton(zbs);
            //myXNAButton. += new EventHandler(myXNAButton_OnPress);
            // button = new MyXNAButton(Content, "khungbao", 1, new Vector2(100, 100), new Vector2(80, 80));
            //nMyButton = 1;
        }

        void myXNAButton_Clicked(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        void myXNAButton_OnPress(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }



        float t = 0;// time counter
        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        float Rx = 50.0f;
        float Ry = 50.0f;
        MyXNAButton temp=null;
        private Vector2 FontPos;
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            
            KeyboardState kbState = Keyboard.GetState();
            map.Update(gameTime);
            //button.Update(gameTime);
            mapcell.Update(gameTime);
            for (int i = 0; i < button.Count; i++)
            {
                //Vector2 pos = button[i].TopLeft;
                //if(pos.X<900)
                //    pos.X++;
                //button[i].TopLeft = pos;
                //button[i].AnimalMove();
                button[i].Update(gameTime);
            }
            MouseState msState = Mouse.GetState();

            myStores.Update(gameTime);
            for (int i = 0; i < myStores.ListZoombie.Count; i++)
                if ( myStores.ListZoombie[i].isClicked(new Vector2(msState.X, msState.Y)))
                {
                        temp = myStores.ListZoombie[i];
                    if (sun.Cost < temp.Cost)
                    
                        temp = null;
                    break;
                }
            text.Text = sun.Cost.ToString();
            //myStores.Cost = sun.Cost;
            text.Update(gameTime);
            sun.Update(gameTime);
            //right.Update(gameTime);
            for(int i =0;i<button.Count;i++)
            {
                Zoombie zb = (Zoombie)button[i];
 //               if (zb.isCollide(right))
                    text.Text = "da bi dung";

            }

            //          isMouseOver = false;

            //MouseState msState = Mouse.GetState();
            //if (msState.LeftButton == ButtonState.Pressed)
            //{
            //    for (int i = 0; i < 12; i++)
            //        for (int j = 0; j < 5; j++)
            //            if (mapcell.Cell[i, j].isClicked(new Vector2(msState.X, msState.Y)))
            //            {
            //                mapcell.Cell[i, j] = new Cell(Content, "Khungbaocomau00", mapcell.Cell[i, j].TopLeft, mapcell.Cell[i, j].Size);

            //            }
            //            else
            //                mapcell.Cell[i, j] = new Cell(Content, "khungbaotrongsuot00", mapcell.Cell[i, j].TopLeft, mapcell.Cell[i, j].Size);

            //}

            /*Vector2 ufo = m.UnknowVar;
            Vector2 vufo = myBTN.TopLeft;
            float mLeft = vufo.X / 2;
            float mRight = vufo.Y / 2;
            Vector2 mPosition = new Vector2(mLeft, mRight);
            if (kbState.IsKeyDown(Keys.Left))
            {
                ufo.X -= 10;
                mPosition.X -= 10;
            }
            if (kbState.IsKeyDown(Keys.Right))
            {

                ufo.X += 10;
                mPosition.X += 10;
            }
            if (kbState.IsKeyDown(Keys.Up))
            {
                ufo.Y -= 10;
                mPosition.Y -= 10;

            }
            if (kbState.IsKeyDown(Keys.Down))
            {
                ufo.Y += 10;
                mPosition.Y += 10;

            }
            m.UnknowVar = ufo;
            myBTN.TopLeft = mPosition;
            if (kbState.IsKeyDown(Keys.Escape))
                this.Exit();
            m.Update(gameTime);
            //myBTN.Update(gameTime);
            //    MouseState msState = Mouse.GetState();
            */
            /*          int idx = -1;
                      if (msState.LeftButton == ButtonState.Pressed)
                      {

                          Vector2 point = new Vector2(msState.X, msState.Y);
                          for (int i = 0; i < nMyButton; i++)
                          {
                              if (myButton[i].isClicked(point))
                              {
                                  idx = i;
                                  //MyXNAButton xna = myButton[idx];

                                  //                        Console.WriteLine(i);
                                  break;
                              }
                          }
                      }
                      switch (idx)
                      {
                          case 0:
                              List<string> str1 = new List<string>();
                              str1.Add("start2");
                              MyXNAButton xna = myButton[idx];
                              xna = new MyXNAButton(Content, str1, str1.Count, xna.TopLeft, xna.Size);
                              myButton[idx] = xna;
                              //                    nMyButton--;

                              //       vtA.X = myButton[idx].TopLeft.X;
                              //      vtA.Y = myButton[idx].TopLeft.Y;
                              break;
                      }
                      /*   Vector2 vtA = new Vector2();
                         switch (idx)
                         { 
                             case 0: 
                                 //myButton[idx] .TopLeft= new Vector2( 100f, 100f);\
                                 vtA.X= myButton[idx].TopLeft.X;
                                 vtA.Y = myButton[idx].TopLeft.Y;
                                 break;
                         }*/
            /*        for (int i = 0; i < nMyButton; i++)
                    {
                        myButton[i].Update(gameTime);
                        /*    Vector2 pos = myButton[i].TopLeft;
                         //phuong trinh duong tron
                        //y=ax+b 
                      * x-xa/yb-ya = y-ya/yb-ya
                      * x= 
                      * //x = a + r cos(t)
                         //y = b + r sin(t).
                         pos.X =(float) (100.0f + Rx * Math.Sin(t/100.0f));

                        pos.Y =(float) (100.0f + Ry * Math.Cos(t/100.0f));
           //              sprites[i].Position = pos;
                        myButton[i].TopLeft = pos;
                         */
            /*      }
                  t++;
                  //            Rx += 0.1f;
                  //          Ry += 0.1f;

                  // TODO: Add your update logic here

                  base.Update(gameTime);
             */
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// 
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            //for (int i = 0; i < nMyButton; i++)
            //  myButton[i].Draw(gameTime, spriteBatch);
            map.Draw(gameTime, spriteBatch);
            //myBTN.Draw(gameTime, spriteBatch);
            // TODO: Add your drawing code here

            //button.Draw(gameTime, spriteBatch);
            mapcell.Draw(gameTime, spriteBatch);
            if (temp != null)
            {
                switch (temp.State)
                {
                    case MyXNAButton.ButtonStatus.Normal:


                        //spriteBatch.Draw(texture, bounds, Color.White);
                        break;
                    case MyXNAButton.ButtonStatus.MouseOver:
                        for (int i = 0; i < 12; i++)
                            for (int j = 0; j < 5; j++)
                                if (mapcell.Cell[i, j].isClicked(new Vector2(temp.PreviousState.X, temp.PreviousState.Y)))
                                {//+ previousState.X - 25


                                    Zoombie btn11 = new Zoombie(Content, "thu", 15, new Vector2(mapcell.Cell[i, j].TopLeft.X, mapcell.Cell[i, j].TopLeft.Y), new Vector2(50, 50), 4, sun);
                                    if (btn11.Cost < sun.Cost)
                                    {
                                        sun.Cost -= btn11.Cost;
                                        //                                    sun.Update(gameTime);

                                        //mapcell.Cell[i, j] = new Cell(Content, "Khungbaocomau00", mapcell.Cell[i, j].TopLeft, mapcell.Cell[i, j].Size);
                                        //   mapcell.Cell[i, j].Update(gameTime);
                                        button.Add(btn11);
                                    }
                                    temp.State = MyXNAButton.ButtonStatus.Normal;

                                    mapcell.Cell[i, j] = new Cell(Content, "khungbaotrongsuot00", mapcell.Cell[i, j].TopLeft, mapcell.Cell[i, j].Size);
                                    //break;
                                }
                                else
                                    mapcell.Cell[i, j] = new Cell(Content, "Khungbaotrongsuot00", mapcell.Cell[i, j].TopLeft, mapcell.Cell[i, j].Size);
                        ///spriteBatch.Draw(hoverTexture, bounds, Color.White);
                        break;
                    case MyXNAButton.ButtonStatus.Pressed:

                        MyXNAButton btn2 = new MyXNAButton(Content, "thu", 1, new Vector2(temp.PreviousState.X, temp.PreviousState.Y), new Vector2(50, 50));
                        btn2.Draw(gameTime, spriteBatch);
                        
                        for (int i = 0; i < 12; i++)
                            for (int j = 0; j < 5; j++)
                                if (mapcell.Cell[i, j].isClicked(new Vector2(temp.PreviousState.X, temp.PreviousState.Y)))
                                {//+ previousState.X - 25

                                    mapcell.Cell[i, j] = new Cell(Content, "Khungbaocomau00", mapcell.Cell[i, j].TopLeft, mapcell.Cell[i, j].Size);

                                    //                                break;
                                }
                                else
                                    mapcell.Cell[i, j] = new Cell(Content, "khungbao00", mapcell.Cell[i, j].TopLeft, mapcell.Cell[i, j].Size);
//                        temp.State = MyXNAButton.ButtonStatus.Pressed;

                        //spriteBatch.Draw(pressedTexture, bounds, Color.White);
                        break;
                    default:
                        // spriteBatch.Draw(texture, bounds, Color.White);
                        break;
                }
            }

            //if (temp != null)
            //{
            //    switch (temp.State)
            //    {
            //        case MyXNAButton.ButtonStatus.Normal:


            //            //spriteBatch.Draw(texture, bounds, Color.White);
            //            break;
            //        case MyXNAButton.ButtonStatus.MouseOver:
            //            for (int i = 0; i < 12; i++)
            //                for (int j = 0; j < 5; j++)
            //                    if (mapcell.Cell[i, j].isClicked(new Vector2(temp.PreviousState.X, temp.PreviousState.Y)))
            //                    {//+ previousState.X - 25
                                        
                                    
            //                        Zoombie btn11 = new Zoombie(Content, "thu", 15, new Vector2(mapcell.Cell[i, j].TopLeft.X, mapcell.Cell[i, j].TopLeft.Y), new Vector2(50, 50),4,sun);
            //                        if (btn11.Cost < sun.Cost)
            //                        {
            //                            sun.Cost -= btn11.Cost;
            //                            //                                    sun.Update(gameTime);

            //                            //mapcell.Cell[i, j] = new Cell(Content, "Khungbaocomau00", mapcell.Cell[i, j].TopLeft, mapcell.Cell[i, j].Size);
            //                            //   mapcell.Cell[i, j].Update(gameTime);
            //                            button.Add(btn11);
            //                        }
            //                        temp.State = MyXNAButton.ButtonStatus.not;

            //                        mapcell.Cell[i, j] = new Cell(Content, "khungbaotrongsuot00", mapcell.Cell[i, j].TopLeft, mapcell.Cell[i, j].Size);
            //                        //break;
            //                    }
            //                    else
            //                        mapcell.Cell[i, j] = new Cell(Content, "Khungbaotrongsuot00", mapcell.Cell[i, j].TopLeft, mapcell.Cell[i, j].Size);
            //            ///spriteBatch.Draw(hoverTexture, bounds, Color.White);
            //            break;
            //        case MyXNAButton.ButtonStatus.Pressed:

            //            MyXNAButton btn2 = new MyXNAButton(Content, "thu", 1, new Vector2(temp.PreviousState.X, temp.PreviousState.Y), new Vector2(50, 50));
            //            btn2.Draw(gameTime, spriteBatch);

            //            for (int i = 0; i < 12; i++)
            //                for (int j = 0; j < 5; j++)
            //                    if (mapcell.Cell[i, j].isClicked(new Vector2(temp.PreviousState.X, temp.PreviousState.Y)))
            //                    {//+ previousState.X - 25

            //                        mapcell.Cell[i, j] = new Cell(Content, "Khungbaocomau00", mapcell.Cell[i, j].TopLeft, mapcell.Cell[i, j].Size);

            //                        //                                break;
            //                    }
            //                    else
            //                        mapcell.Cell[i, j] = new Cell(Content, "khungbao00", mapcell.Cell[i, j].TopLeft, mapcell.Cell[i, j].Size);

            //            //spriteBatch.Draw(pressedTexture, bounds, Color.White);
            //            break;
            //        default:
            //            // spriteBatch.Draw(texture, bounds, Color.White);
            //            break;
            //    }
            //}
            myStores.Draw(gameTime, spriteBatch);

            for (int i = 0; i < button.Count; i++)
                button[i].Draw(gameTime, spriteBatch);
            text.Draw(gameTime, spriteBatch);
            sun.Draw(gameTime, spriteBatch);
            //right.Draw(gameTime, spriteBatch);
            //sun.Draw(gameTime, spriteBatch); 
            base.Draw(gameTime);
            
        }
        
    }
}
